<?php

namespace app\api\model\wanlshop;

use app\index\model\wanlshop\ActivityGoodsSku;
use think\Model;
use think\Request;
use traits\model\SoftDelete;

class GoodsSku extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'wanlshop_goods_sku';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
	
	
	// getDifferenceAttr
	public function getDifferenceAttr($value)
	{	
		return $value ? explode(',', $value) : [];
	}




    //获取sku对应会员价格
    public function getSkuMember($goods_id){
	    $list = $this->where([
	        'goods_id' => $goods_id,
            'state' => 0
        ])->select();
        return $this->getGroupSku($list,$goods_id);
    }

    //转换sku内部会员价格,并进行中英文切换
    public function getGroupSku($list,$goods_id){
        $auth = \app\common\library\Auth::instance();
        $group_id = $auth->group_id;
        $request = Request::instance();
        $lang = $request->langset();
        $currency_rate = model('app\api\model\wanlshop\Pay')->getCurrencyRate('',0);
        //查询该商品有没有活动
        $activity = model('app\api\model\wanlshop\Activity')->checkActivity($goods_id);
        $activitySku = [];
        if($activity){
            $activitySkus = model('app\index\model\wanlshop\ActivityGoodsSku')->where([
                'activity_id' => $activity['id'],
                'goods_id' => $goods_id,
            ])->select();
            foreach (collection($activitySkus)->toArray() as $skus){
                $activitySku[$skus['sku_id']] = $skus;
            }
            $rules = json_decode($activity['rules'],true);
        }

        if(empty($list->group_price)){

            foreach ($list as $item){
                if($lang == 'en'){
                    $item['difference'] = $item['difference_en'] ? $item['difference_en'] : $item['difference'];
                }
                if(isset($activitySku[$item['id']]) && $activitySku[$item['id']]['stock'] > 0){
                    $item['price'] = $activitySku[$item['id']]['price'];
                    $item['rmb_price'] = bcmul($activitySku[$item['id']]['price'], $currency_rate, 2);
                    $item['stock'] = $activitySku[$item['id']]['stock'];
                    $item['activity'] = $activity['type'];
                    $item['activity_id'] = $activity['id'];
                    $item['activity_info'] = $activity;
                    $item['limit'] = (int) $rules['limit'];
                }else{
                    $group_price = $item['group_price'];
                    if(!is_array($group_price)){
                        $group_price = json_decode($group_price,true);
                    }
                    $group_price1 = array_column($group_price,'price');


                    $group_price = array_column($group_price,'price','group_id');

                    $item['price'] = max($group_price1);
                    $item['price'] = $group_price[$group_id] ?? $item['price'];
                    $item['rmb_price'] = bcmul(($group_price[$group_id] ?? $item['price']), $currency_rate, 2);
                    $item['activity'] = false;
                    $item['activity_id'] = 0;
                    $item['limit'] = 0;
                    $item['activity_info'] = [];
                }
            }
        }else{
            if($lang == 'en'){
                $list['difference'] = $list['difference_en'] ? $list['difference_en'] :$list['difference'];
            }
            if(isset($activitySku[$list['id']]) && $activitySku[$list['id']]['stock'] > 0){
                $list['price'] = $activitySku[$list['id']]['price'];
                $list['rmb_price'] = bcmul($activitySku[$list['id']]['price'], $currency_rate, 2);
                $list['stock'] = $activitySku[$list['id']]['stock'];
                $list['activity'] = $activity['type'];
                $list['activity_id'] = $activity['id'];
                $list['limit'] = (int) $rules['limit'];
                $item['activity_info'] = $activity;

            }else{
                $group_price = $list['group_price'];
                if(!is_array($group_price)){
                    $group_price = json_decode($group_price,true);
                }
                $group_price1 = array_column($group_price,'price');
                $list['price'] = max($group_price1);
                $group_price = array_column($group_price,'price','group_id');
                $list['price'] = $group_price[$group_id] ?? $list['price'];
                $list['rmb_price'] = bcmul(($group_price[$group_id] ?? $list['price']), $currency_rate, 2);
                $list['activity'] = false;
                $list['activity_id'] = 0;
                $list['limit'] = 0;
                $item['activity_info'] = [];
            }

        }
        return $list;
    }

}
